<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :xs="24" :sm="12">
            <a-form-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['materielCode', validatorRules.materielCode]" placeholder="请输入料号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productCode']" placeholder="请输入商品编码"></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :xs="24" :sm="12">
            <a-form-item label="物料组编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag v-decorator="['mgroupCode']" dict="mes_chiefdata_materielgroup,group_code,group_code" />
            </a-form-item>
          </a-col> -->
           <a-col :xs="24" :sm="12">
            <a-form-item label="线别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['mgroupCode']" :trigger-change="true" dictCode="mgroup_code" placeholder="请选择线别"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="品名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productName']" placeholder="请输入品名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['gauge']" placeholder="请输入规格"></a-input>
            </a-form-item>
          </a-col>
         <!-- <a-col :xs="24" :sm="12">
            <a-form-item label="物料类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['materielSort', validatorRules.materielSort]" :trigger-change="true" dictCode="materiel_sort" placeholder="请选择物料类别"/>
            </a-form-item>
          </a-col> -->
          <a-col :xs="24" :sm="12">
            <a-form-item label="物料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['materielType', validatorRules.materielType]" :trigger-change="true" dictCode="materiel_type" placeholder="请选择物料类型"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="物料来源" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['materielSource']" :trigger-change="true" dictCode="materiel_source" placeholder="请选择物料来源"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unit', validatorRules.unit]" placeholder="请输入单位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="产品条码规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['barcodeRule']" placeholder="请输入产品条码规则"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['client']"
                :trigger-change="true"
                org-fields="id,client_name"
                dest-fields="clientId,client"
                code="client_search"
                @callback="popupCallback"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-popup
                v-decorator="['supplier']"
                :trigger-change="true"
                org-fields="id,supplier_name"
                dest-fields="supplierId,supplier"
                code="supplier_search"
                @callback="popupCallback"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="客户料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['query2']" placeholder="请输入客户料号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="单重" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unitWeight']" placeholder="请输入单重"></a-input>
            </a-form-item>
          </a-col>
         <!-- <a-col :xs="24" :sm="12">
            <a-form-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sectionNo']" placeholder="请输入批次号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="公司编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['companyCode']" placeholder="请输入公司编码"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :xs="24" :sm="12">
            <a-form-item label="是否锁定" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['ifLock']" :trigger-change="true" dictCode="yn" placeholder="请选择是否锁定"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['note']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
          </a-col>
          <a-form-item v-show="false" label="客户ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-show="false" v-decorator="['clientId']" placeholder="请输入客户ID"></a-input>
          </a-form-item>
          <a-form-item v-show="false" label="供应商ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-show="false" v-decorator="['supplierId']" placeholder="请输入供应商ID"></a-input>
          </a-form-item>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="物料—PCB" :key="refKeys[0]" :forceRender="true">
          <mes-materiel-pcb-form ref="mesMaterielPcbForm" @validateError="validateError"></mes-materiel-pcb-form>
        </a-tab-pane>
      
        <a-tab-pane tab="主数据—辅料-钢网/刮刀" :key="refKeys[1]" :forceRender="true">
          <mes-materiel-netknife-form ref="mesMaterielNetknifeForm" @validateError="validateError"></mes-materiel-netknife-form>
        </a-tab-pane>
      
        <a-tab-pane tab="物料—生产" :key="refKeys[2]" :forceRender="true">
          <mes-materiel-produce-form ref="mesMaterielProduceForm" @validateError="validateError"></mes-materiel-produce-form>
        </a-tab-pane>
      
        <a-tab-pane tab="主数据—辅料-红胶/锡膏" :key="refKeys[3]" :forceRender="true">
          <mes-materiel-redgum-form ref="mesMaterielRedgumForm" @validateError="validateError"></mes-materiel-redgum-form>
        </a-tab-pane>
      
        <a-tab-pane tab="物料—品质" :key="refKeys[4]" :forceRender="true">
          <mes-materiel-quality-form ref="mesMaterielQualityForm" @validateError="validateError"></mes-materiel-quality-form>
        </a-tab-pane>
      
        <a-tab-pane tab="物料—仓储" :key="refKeys[5]" :forceRender="true">
          <mes-materiel-storage-form ref="mesMaterielStorageForm" @validateError="validateError"></mes-materiel-storage-form>
        </a-tab-pane>
      
        <a-tab-pane tab="物料—其他" :key="refKeys[6]" :forceRender="true">
          <mes-materiel-other-form ref="mesMaterielOtherForm" @validateError="validateError"></mes-materiel-other-form>
        </a-tab-pane>
        
      
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import MesMaterielPcbForm from './MesMaterielPcbForm.vue'
  import MesMaterielNetknifeForm from './MesMaterielNetknifeForm.vue'
  import MesMaterielProduceForm from './MesMaterielProduceForm.vue'
  import MesMaterielRedgumForm from './MesMaterielRedgumForm.vue'
  import MesMaterielQualityForm from './MesMaterielQualityForm.vue'
  import MesMaterielStorageForm from './MesMaterielStorageForm.vue'
  import MesMaterielOtherForm from './MesMaterielOtherForm.vue'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'

  export default {
    name: 'MesChiefdataMaterielModal',
    mixins: [JEditableTableMixin],
    components: {
    MesMaterielPcbForm,
    MesMaterielNetknifeForm,
    MesMaterielProduceForm,
    MesMaterielRedgumForm,
    MesMaterielQualityForm,
    MesMaterielStorageForm,
    MesMaterielOtherForm,
      JDictSelectTag,
      JSearchSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          materielCode: {
            rules: [
              { required: true, message: '请输入料号!'},
            ]
          },
          materielSort: {
            rules: [
              { required: true, message: '请输入物料类别!'},
            ]
          },
          materielType: {
            rules: [
              { required: true, message: '物料类型不能为空!'},
            ]
          },
          unit: {
            rules: [
              { required: true, message: '单位不能为空!'},
            ]
          },
        },
        refKeys: ['mesMaterielPcb', 'mesMaterielNetknife', 'mesMaterielProduce', 'mesMaterielRedgum', 'mesMaterielQuality', 'mesMaterielStorage', 'mesMaterielOther', ],
        tableKeys:[],
        activeKey: 'mesMaterielPcb',
        // 物料—PCB
        mesMaterielPcbTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 主数据—辅料-钢网/刮刀
        mesMaterielNetknifeTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 物料—生产
        mesMaterielProduceTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 主数据—辅料-红胶/锡膏
        mesMaterielRedgumTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 物料—品质
        mesMaterielQualityTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 物料—仓储
        mesMaterielStorageTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        // 物料—其他
        mesMaterielOtherTable: {
          loading: false,
          dataSource: [],
          columns: [
          ]
        },
        url: {
          add: "/chiefdata/mesChiefdataMateriel/add",
          edit: "/chiefdata/mesChiefdataMateriel/edit",
          mesMaterielPcb: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielPcbByMainId'
          },
          mesMaterielNetknife: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielNetknifeByMainId'
          },
          mesMaterielProduce: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielProduceByMainId'
          },
          mesMaterielRedgum: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielRedgumByMainId'
          },
          mesMaterielQuality: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielQualityByMainId'
          },
          mesMaterielStorage: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielStorageByMainId'
          },
          mesMaterielOther: {
            list: '/chiefdata/mesChiefdataMateriel/queryMesMaterielOtherByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        return new Promise(resolve => {
          resolve([]);
        })
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'materielCode','productCode','mgroupCode','productName','gauge','materielSort','materielType','materielSource','unit','barcodeRule','client','supplier','query2','unitWeight','sectionNo','companyCode', 'ifLock','note','clientId','supplierId')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
          this.$refs.mesMaterielPcbForm.initFormData(this.url.mesMaterielPcb.list,this.model.id)
          this.$refs.mesMaterielNetknifeForm.initFormData(this.url.mesMaterielNetknife.list,this.model.id)
          this.$refs.mesMaterielProduceForm.initFormData(this.url.mesMaterielProduce.list,this.model.id)
          this.$refs.mesMaterielRedgumForm.initFormData(this.url.mesMaterielRedgum.list,this.model.id)
          this.$refs.mesMaterielQualityForm.initFormData(this.url.mesMaterielQuality.list,this.model.id)
          this.$refs.mesMaterielStorageForm.initFormData(this.url.mesMaterielStorage.list,this.model.id)
          this.$refs.mesMaterielOtherForm.initFormData(this.url.mesMaterielOther.list,this.model.id)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          mesMaterielPcbList: this.$refs.mesMaterielPcbForm.getFormData(),
          mesMaterielNetknifeList: this.$refs.mesMaterielNetknifeForm.getFormData(),
          mesMaterielProduceList: this.$refs.mesMaterielProduceForm.getFormData(),
          mesMaterielRedgumList: this.$refs.mesMaterielRedgumForm.getFormData(),
          mesMaterielQualityList: this.$refs.mesMaterielQualityForm.getFormData(),
          mesMaterielStorageList: this.$refs.mesMaterielStorageForm.getFormData(),
          mesMaterielOtherList: this.$refs.mesMaterielOtherForm.getFormData(),
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'materielCode','productCode','mgroupCode','productName','gauge','materielSort','materielType','materielSource','unit','barcodeRule','client','supplier','query2','unitWeight','sectionNo','companyCode','ifLock','note','clientId','supplierId'))
     },

    }
  }
</script>

<style scoped>
</style>